package contoller;

import com.google.gson.Gson;
import entity.SysUserEntity;
import model.SysUserService;
import model.SysUserServiceImp;
import util.DateUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2018/4/30.
 */
@WebServlet(name = "UserServlet",
urlPatterns = {"/UserServlet"})
public class UserServlet extends HttpServlet {
    private SysUserService sysUserService=new SysUserServiceImp();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        System.out.println(action);
        if (action.equals("query")) {
            this.query(request,response);
        } else if(action.equals("insert")) {
            this.insert(request,response);
        } else if(action.equals("update")) {
            this.update(request,response);
        } else if(action.equals("delete")) {
            this.delete(request,response);
        }
    }
    public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置获取参数的编码方式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        //获取参数
        String username=request.getParameter("username");
        String page=request.getParameter("page");
        String limit=request.getParameter("limit");
        System.out.println(username+page+limit);
        //将参数放在MAP对象中
        Map<String,Object> params=new HashMap<String,Object>();
        if(username!=null && !username.equals("")) {
            params.put("username",username);
        }
        if(page!=null) {    //page:查询页码
            params.put("page",Integer.parseInt(page));
        }
        if(limit!=null) {    //limit:每页显示的数据量
            params.put("limit",Integer.parseInt(limit));
        }
        List<SysUserEntity> userList=sysUserService.queryUser(params);
        int count=sysUserService.queryUserCount(params);
        Gson gson=new Gson();
        Map<String, Object> userMap = new HashMap<String, Object>();
        userMap.put("code", 0);
        userMap.put("msg", "");
        userMap.put("count",count);
        userMap.put("data",userList);
        System.out.println(gson.toJson(userMap));
        out.print(gson.toJson(userMap));
        out.flush();
        out.close();
    }
    public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置获取参数的编码方式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        //获取参数
        String username=request.getParameter("username");
        String email=request.getParameter("email");
        String tel=request.getParameter("tel");
        String status=request.getParameter("status");
        SysUserEntity user=new SysUserEntity();
        user.setEmail(email);
        user.setUsername(username);
        user.setTel(tel);
        user.setStatus(Integer.parseInt(status));
        //获取插入时间
        String time= DateUtils.formatTime(new Date());
        user.setCreateTime(time);
        int rtn=sysUserService.insertUser(user);
        if(rtn>0) {
            out.print("新增用户成功");
        } else {
            out.print("新增用户失败");
        }
        out.flush();
        out.close();
    }
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置获取参数的编码方式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        //获取参数
        String userIds=request.getParameter("userIds");
        //以,分隔
        String[] idStrArr=userIds.split(",");
        Integer[] idIntArr=new Integer[idStrArr.length];
        int i=0;
        for(i=0;i<idIntArr.length;i++) {
            idIntArr[i]=Integer.parseInt(idStrArr[i]);
        }
        sysUserService.deleteBatchUser(idIntArr);
        out.print("删除用户成功");
        out.flush();
        out.close();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
